package com.reading_recognition_system.controller;

import com.reading_recognition_system.pojo.History;
import com.reading_recognition_system.service.impl.HistoryServiceImpl;
import com.reading_recognition_system.util.Result;
import com.reading_recognition_system.util.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@RequestMapping("/history")
public class HistoryController {
    @Autowired
    private HistoryServiceImpl historyService;

    //查询历史记录
    @RequestMapping("/findhistory")
    @ResponseBody
    public Result<History> findhistory(int u_id){
        History res = historyService.findHistory(u_id);
        return new Result<>(StatusCode.SUCCESS , res,"成功");
    }

    //插入历史记录
    @RequestMapping("/inserthistory")
    @ResponseBody
    public Result<String> inserthistory(@RequestBody History history){
        if (historyService.insertHistory(history) != 0 ){
            return new Result<>(StatusCode.SUCCESS , "插入成功","成功");
        }else {
            return new Result<>(StatusCode.EXCEPTION , "插入失败","成功");
        }
    }

    //删除全部历史记录
    @RequestMapping("/deleteallhistory")
    @ResponseBody
    public Result<String> deleteallhistory(int u_id){
        if(historyService.deleteAllHistory(u_id) != 0){
            return new Result<>(StatusCode.SUCCESS , "删除成功","成功");
        }else {
            return new Result<>(StatusCode.EXCEPTION , "删除失败","失败");
        }
    }

    //删除具体某一个历史记录
    @RequestMapping("/deleteonehistory")
    @ResponseBody
    public Result<String> deleteonehistory(int h_id, int u_id){
        if (historyService.deleteOneHistory(h_id, u_id) != 0){
            return new Result<>(StatusCode.SUCCESS , "删除成功","成功");
        }else{
            return new Result<>(StatusCode.EXCEPTION , "删除失败","失败");
        }
    }

    //删除某一时间段的历史记录
    @RequestMapping("/deletetimehistory")
    @ResponseBody
    public Result<String> deletetimehistory(Date timestamp){
        if (historyService.deleteTimeHistory(timestamp) != 0){
            return new Result<>(StatusCode.SUCCESS , "删除成功","成功");
        }else{
            return new Result<>(StatusCode.EXCEPTION , "删除失败","失败");
        }
    }
}
